const jwt = require('jsonwebtoken');
const _ = require('underscore');
const path = require('path');
const fs = require('fs');
const keyPath = path.join(__dirname, '../../../file/serc.key');
const privateKey = fs.readFileSync(keyPath);
let functions = {
    createToken: function (payload) {   //创建令牌
        let token = jwt.sign(payload, privateKey);
        return token;
    },
    verified: function (token) {       //核对令牌
        try {
            let decoded = jwt.verify(token, privateKey);
            return {success: true, decoded: decoded};
        } catch (e) {
            return {success: false, message: e.name};
        }
    }
};
module.exports = functions;
